﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCDanhSachHoaDonBan : UserControl
    {
        String id_HoaDonBan;
        String idnhanvien;
        public UCDanhSachHoaDonBan(String idtoancuc)
        {
            InitializeComponent();
            this.idnhanvien = idtoancuc;
            showDanhSachHoaDonBan();
        }
        /// <summary>
        /// Hiển thị danh sách hóa đơn bán
        /// </summary>
        void showDanhSachHoaDonBan()
        {
            HoaDonBanBUO hdbanbuo=new HoaDonBanBUO();
            dataGridView1.DataSource = hdbanbuo.ShowHoaDonBan();
            dataGridView1.AutoGenerateColumns = false;
        }

        private void rdTimTheoMaDDH_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTimTheoMaHoaDon.Checked==true)
            {
                txtTimTheoMaHD.Enabled = true;
                txtTimTheoKhach.Enabled = false;
                dateTimePicker1.Enabled = false;
            }
        }

        private void rdTimTheoKhach_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTimTheoKhach.Checked == true)
            {
                txtTimTheoMaHD.Enabled = false;
                txtTimTheoKhach.Enabled = true;
                dateTimePicker1.Enabled = false;
            }
        }

        private void rdTimTheoNgay_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTimTheoNgay.Checked == true)
            {
                txtTimTheoMaHD.Enabled = false;
                txtTimTheoKhach.Enabled = false;
                dateTimePicker1.Enabled = true;
            }
        }

        private void txtTimTheoMaHD_KeyPress(object sender, KeyPressEventArgs e)
        {
            HoaDonBanBUO hdbanbuo = new HoaDonBanBUO();
            dataGridView1.DataSource = hdbanbuo.SearchByMaHDBan(txtTimTheoMaHD.Text);
        }

        private void txtTimTheoMaHD_KeyDown(object sender, KeyEventArgs e)
        {
            HoaDonBanBUO hdbanbuo = new HoaDonBanBUO();
            dataGridView1.DataSource = hdbanbuo.SearchByMaHDBan(txtTimTheoMaHD.Text);
        }

        private void txtTimTheoKhach_KeyPress(object sender, KeyPressEventArgs e)
        {
            HoaDonBanBUO hdbanbuo = new HoaDonBanBUO();
            dataGridView1.DataSource = hdbanbuo.SearchByKhach(txtTimTheoKhach.Text);
        }

        private void txtTimTheoKhach_KeyDown(object sender, KeyEventArgs e)
        {
            HoaDonBanBUO hdbanbuo = new HoaDonBanBUO();
            dataGridView1.DataSource = hdbanbuo.SearchByKhach(txtTimTheoKhach.Text);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (rdTimTheoMaHoaDon.Checked==true)
            {
                HoaDonBanBUO hdbanbuo = new HoaDonBanBUO();
                dataGridView1.DataSource = hdbanbuo.SearchByMaHDBan(txtTimTheoMaHD.Text);
            }

            if (rdTimTheoKhach.Checked==true)
            {
                HoaDonBanBUO hdbanbuo = new HoaDonBanBUO();
                dataGridView1.DataSource = hdbanbuo.SearchByKhach(txtTimTheoKhach.Text);
            }

            if (rdTimTheoNgay.Checked==true)
            {
                HoaDonBanBUO hdbanbuo = new HoaDonBanBUO();
                dataGridView1.DataSource = hdbanbuo.SearchByNgayThang(dateTimePicker1.Text);
            }            
        }

        private void dateTimePicker1_MouseDown(object sender, MouseEventArgs e)
        {
            
            
        }

        private void dateTimePicker1_MouseEnter(object sender, EventArgs e)
        {
            HoaDonBanBUO hdbanbuo = new HoaDonBanBUO();
            dataGridView1.DataSource = hdbanbuo.SearchByNgayThang(dateTimePicker1.Text);
        }
        //thêm hóa đơn bán
        private void thêmHóaĐơnBánToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = this.ParentForm;
            UCThemHoaDonBan ucThemHDBan = new UCThemHoaDonBan(idnhanvien);
            ControlAccess.ShowConTrol(form, ucThemHDBan, "grbHienThiChucNang");
        }
        //sửa hóa đơn bán
        private void sửaHóaĐơnBánToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = this.ParentForm;
            UCSuaHoaDonBan ucSuaHDBan = new UCSuaHoaDonBan(id_HoaDonBan);
            ControlAccess.ShowConTrol(form, ucSuaHDBan, "grbHienThiChucNang");
        }
        //lấy id hóa đơn bán
        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        id_HoaDonBan = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void xóaHóaĐơnBánToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HoaDonBanBUO hdbanbuo = new HoaDonBanBUO();
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (hdbanbuo.Delete(id_HoaDonBan))
                {
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                showDanhSachHoaDonBan();
            }
        }
    }
}
